package com.thlws.demojooq.config;

import org.jooq.SQLDialect;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * 本文配置内容参考官方说明 http://www.jooq.org/doc/3.11/manual-single-page/#jooq-with-spring
 * JOOQ 数据源及事务配置
 * Created by  HanleyTang on 2019-01-12
 */
@Configuration
public class SpringConfig {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final DbConfig dbConfig;

    @Autowired
    public SpringConfig(DbConfig dbConfig) {
        this.dbConfig = dbConfig;
    }

    @PostConstruct
    private void init() {
        logger.debug("初始化db信息......");
    }

    /**
     * 原始数据库配置
     */
    @Bean(name = "originDataSource")
    public DataSource originDataSource(){
        return DataSourceBuilder.create()
                .driverClassName(dbConfig.getDriver_class_name())
                .password(dbConfig.getPassword())
                .username(dbConfig.getUsername())
                .url(dbConfig.getUrl()).build();
    }

    /***
     * 配置事务管理器
     */
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("demoDataSource") TransactionAwareDataSourceProxy dynamicDataSource){
        return new DataSourceTransactionManager(dynamicDataSource);
    }


    /***
     * 配置数据源代理
     */
    @Bean("demoDataSource")
    public TransactionAwareDataSourceProxy transactionAwareDataSourceProxy(@Qualifier("originDataSource") DataSource originDataSource){
        return new TransactionAwareDataSourceProxy(originDataSource);
    }

    /**
     * 配置JOOQ数据源连接器
     */
    @Bean("myConnectionProvider")
    public DataSourceConnectionProvider jooqConnectionProvider(@Qualifier("demoDataSource") TransactionAwareDataSourceProxy dynamicDataSource){
        DataSourceConnectionProvider provider = new DataSourceConnectionProvider(dynamicDataSource);
        return provider;
    }

    @Bean("myDSLContext")
    public DefaultDSLContext jooqDSLContext(@Qualifier("myConfiguration") DefaultConfiguration jooqConfiguration ){
        return new DefaultDSLContext(jooqConfiguration);
    }


    @Bean("myConfiguration")
    public DefaultConfiguration jooqConfiguration(@Qualifier("myConnectionProvider") DataSourceConnectionProvider myConnectionProvider,
                                                  @Qualifier("myExecuteListenerProvider") DefaultExecuteListenerProvider myExecuteListenerProvider){

        DefaultConfiguration configuration = new DefaultConfiguration();
        configuration.setConnectionProvider(myConnectionProvider);
        configuration.setSQLDialect(SQLDialect.MYSQL);//FIXME 注意这里要设置当前数据库类型.
        configuration.setExecuteListenerProvider(myExecuteListenerProvider);
        return configuration;
    }

    @Bean("myExecuteListenerProvider")
    public DefaultExecuteListenerProvider executeListenerProvider(@Qualifier("myExceptionTranslator") ExceptionTranslator exceptionTranslator){
        return new DefaultExecuteListenerProvider(exceptionTranslator);
    }

    @Bean("myExceptionTranslator")
    public ExceptionTranslator exceptionTranslator(){
        return new ExceptionTranslator();
    }




}
